package com.example.springboot_test.common.poi.util;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * POI转换工具类
 *
 * @author xudongmaster
 */
public class POIConvertUtil {
    /**
     * XWPFDocument 转 MultipartFile（CommonsMultipartFile）
     *
     * @param document 文档对象
     * @param fileName 文件名
     * @return MultipartFile
     */
    public static MultipartFile xwpfDocumentToCommonsMultipartFile(XWPFDocument document, String fileName) {
        //XWPFDocument转FileItem
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem fileItem = factory.createItem("textField", "text/plain", true, fileName);
        try {
            OutputStream os = fileItem.getOutputStream();
            document.write(os);
            os.close();
            //FileItem转MultipartFile
            return new CommonsMultipartFile(fileItem);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * XWPFDocument 转 MultipartFile(MockMultipartFile)
     *
     * @param document 文档对象
     * @param fileName 文件名
     * @return MultipartFile
     */
    public static MultipartFile xwpfDocumentToMockMultipartFile(XWPFDocument document, String fileName) {
        try {
            String contentType = "text/plain";
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            document.write(bos);
            //XWPFDocument 转 byte[]
            byte[] byteArray = bos.toByteArray();
            //byte[] 转 InputStream
            InputStream is = new ByteArrayInputStream(byteArray);
            //InputStream 转 MultipartFile
            return new MockMultipartFile(fileName, fileName, contentType, is);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Workbook 转 MultipartFile(CommonsMultipartFile)
     *
     * @param workbook excel文档
     * @param fileName 文件名
     * @return MultipartFile
     */
    public static MultipartFile workbookToCommonsMultipartFile(Workbook workbook, String fileName) {
        //Workbook转FileItem
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem fileItem = factory.createItem("textField", "text/plain", true, fileName);
        try {
            OutputStream os = fileItem.getOutputStream();
            workbook.write(os);
            os.close();
            //FileItem转MultipartFile
            return new CommonsMultipartFile(fileItem);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Workbook 转 MultipartFile(MockMultipartFile)
     *
     * @param workbook excel文档
     * @param fileName 文件名
     * @return MultipartFile
     */
    public static MultipartFile workbookToMockMultipartFile(Workbook workbook, String fileName) {
        try {
            String contentType = "text/plain";
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            workbook.write(bos);
            //Workbook 转 byte[]
            byte[] byteArray = bos.toByteArray();
            //byte[] 转 InputStream
            InputStream is = new ByteArrayInputStream(byteArray);
            //InputStream 转 MultipartFile
            return new MockMultipartFile(fileName, fileName, contentType, is);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
